package com.jy.unkown.page;

import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.amjy.dsl.R;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.PlayerView;
import com.jy.unkown.AdCallBack;
import com.jy.unkown.UNKOWN;
import com.jy.unkown.entity.UnkownAdBean;
import com.jy.unkown.inter.IRewardCallBack;
import com.jy.unkown.view.UnkowVideoView;
import com.jy.unkown.view.UnkownRewardVideoView;

public class RewardActivity extends BUnkownActivity implements IRewardCallBack {
    FrameLayout container;

    PlayerView playerView;
    UnkownRewardVideoView unkownRewardVideoView;

    @Override
    public int layoutId() {
        return R.layout.activity_reward_video_unkown;
    }

    @Override
    public void init() {
        container = findViewById(R.id.container);
        unkownRewardVideoView = new UnkownRewardVideoView(this);
        container.addView(unkownRewardVideoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        playerView = unkownRewardVideoView.findViewById(R.id.unkownVideoView);

        UnkownAdBean unkownAdBean = UNKOWN.getAd(getKey());
        if (unkownAdBean == null) {
            finish();
            return;
        }
        unkownRewardVideoView.setAdCallBack(this);
        unkownRewardVideoView.setUnkownAd(unkownAdBean);
        unkownRewardVideoView.show();
    }

    private void initPlayerView() {
        if (playerView == null) {
            playerView = findViewById(R.id.unkownVideoView);
        }
    }

    private String getKey() {
        String key = getIntent().getStringExtra(UNKOWN.UNKOWN_AD_KEY);
        return key;
    }


    @Override
    public void err() {
        AdCallBack adCallBack = UNKOWN.getAdCallBack(getKey());
        if (adCallBack != null) {
            adCallBack.err();
        }
    }

    @Override
    public void click() {
        AdCallBack adCallBack = UNKOWN.getAdCallBack(getKey());
        if (adCallBack != null) {
            if (adCallBack instanceof IRewardCallBack) {
                ((IRewardCallBack) adCallBack).click();
            }

        }
    }

    @Override
    public void close() {
        AdCallBack adCallBack = UNKOWN.getAdCallBack(getKey());
        if (adCallBack != null) {
            if (adCallBack instanceof IRewardCallBack) {
                ((IRewardCallBack) adCallBack).close();
            }
            finish();
        }
    }

    @Override
    public void exposure() {
        AdCallBack adCallBack = UNKOWN.getAdCallBack(getKey());
        if (adCallBack != null) {
            if (adCallBack instanceof IRewardCallBack) {
                ((IRewardCallBack) adCallBack).exposure();
            }

        }
    }

    @Override
    public void reward() {
        AdCallBack adCallBack = UNKOWN.getAdCallBack(getKey());
        if (adCallBack != null) {
            if (adCallBack instanceof IRewardCallBack) {
                ((IRewardCallBack) adCallBack).reward();
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPlayerView();
        if (playerView != null) {
            Player player = playerView.getPlayer();
            if (!player.isPlaying()) {
                playerView.getPlayer().setPlayWhenReady(true);
            }

        }
        unkownRewardVideoView.resume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        initPlayerView();
        if (playerView != null) {
            Player player = playerView.getPlayer();
            if (player.isPlaying()) {
                playerView.getPlayer().setPlayWhenReady(false);
            }

        }
        unkownRewardVideoView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        initPlayerView();
        if (playerView != null) {
            Player player = playerView.getPlayer();
            if (player != null) {
                player.release();
            }

        }
        unkownRewardVideoView.onDestory();
    }

    @Override
    public void skip() {
        AdCallBack adCallBack = UNKOWN.getAdCallBack(getKey());
        if (adCallBack != null) {
            if (adCallBack instanceof IRewardCallBack) {
                ((IRewardCallBack) adCallBack).skip();
            }

        }
    }
}
